package com.dctobacco.dongchengtobaccoandroid.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dctobacco.dongchengtobaccoandroid.R;
import com.dctobacco.dongchengtobaccoandroid.widget.CircleImageView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MenuFragment extends Fragment implements RadioGroup.OnCheckedChangeListener {

    MenuItemCheckedChangeListener menuItemCheckedChangeListener;
//  Menu
    @Bind(R.id.radio_group)
    RadioGroup radioGroup;
    //  用户电话
    @Bind(R.id.textview_phonenum)
    TextView phonenum_tv;
//    用户头像
    @Bind(R.id.iv_portrait)
    CircleImageView ivPortrait;
//    扫描二维码
    @Bind(R.id.qrCodeScanimg)
    ImageView qrCodeScan;



    //    定义一个接口
    public interface MenuItemCheckedChangeListener {
        //      切换Fragment的按钮
        void onCheckedChanged(int checkedId);

        //      扫描二维码
        void qrCodeScan();

//        跳转到个人中心
        void jumpUserInfoPager();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            menuItemCheckedChangeListener = (MenuItemCheckedChangeListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement MenuItemCheckedChangeListener");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_menu, container, false);
        ButterKnife.bind(this, view);
        radioGroup.setOnCheckedChangeListener(this);
        return view;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        menuItemCheckedChangeListener.onCheckedChanged(checkedId);
    }

    /**
     * @param view
     * 描述：点击头像跳转到个人中心
     *      点击二维码开始扫描二维码
     */
    @OnClick({R.id.iv_portrait, R.id.qrCodeScanimg})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_portrait:
                menuItemCheckedChangeListener.jumpUserInfoPager();
                break;
            case R.id.qrCodeScanimg:
                menuItemCheckedChangeListener.qrCodeScan();
                break;
        }
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
